%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        INETS mod file for ERML
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(mod_erml).
-author('sjackson@simpleenigma.com').
-include_lib("inets/src/httpd.hrl").
-include("../include/erml.hrl").

-export([do/1,load/2]).

do(Mod) -> 
	case is_erml(Mod) of
		true  -> process_erml(Mod);
		false -> {proceed, Mod#mod.data}
	end.

load("ErmlExt " ++ Ext, []) -> {ok, [], {erml_ext, Ext}}.

is_erml(Mod) when is_record(Mod,mod) -> is_erml(Mod#mod.data);
is_erml(Data) -> 
	case find_real_name(Data) of
		{error,none} -> false;
		{filename,FileName} -> 
				Ext = filename:extension(FileName),
				case http_util:to_lower(Ext) of
					".erml" -> 	true;
					_ -> false
				end;
		_ -> false
	end.

process_erml(Mod) -> 
	ERML = mod_to_erml(Mod),
	case erml:scan(ERML) of
		{ok,no_erml_commands} -> {proceed, Mod#mod.data};
		{ok,erml_processed,Body,NewERML} -> 
			Data = lists:append([[{response,{200,Body}}],NewERML#erml.response,Mod#mod.data]),
			{proceed, Data}
	end.

find_real_name([H|T]) ->
	case H of
		{real_name,{FileName,_Query}} -> 
			{filename,FileName};
		_ -> find_real_name(T)
	end;
find_real_name([]) -> {error,none}.

mod_to_erml(Mod) when is_record(Mod,mod) ->
	{filename,FileName} = find_real_name(Mod#mod.data),
	{ok,{IP,_Port}} = inet:peername(Mod#mod.socket),
	{IP1,IP2,IP3,IP4} = IP,
	Address = integer_to_list(IP1) ++ "." ++integer_to_list(IP2) ++ "." ++ integer_to_list(IP3) ++ "." ++ integer_to_list(IP4),
	#erml{
		filename      = FileName,
		socket        = Mod#mod.socket,
		method        = Mod#mod.method,
		absolute_uri  = Mod#mod.absolute_uri,
		request_uri   = Mod#mod.request_uri,
		http_version  = Mod#mod.http_version,
		request_line  = Mod#mod.request_line,
		remove_addr   = Address,
		parsed_header = Mod#mod.parsed_header,
		entity_body   = Mod#mod.entity_body
		}.



